// pages/opt_user_admin/opt_page/produce_list/testDetail/testDetail.js
var app = getApp()
const util = require('../../../../../../utils/util.js')

//常量数据配置
var ConstantData = require('../../../../../../common/constant_data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize:10,
    pageNum:1,
    array:['1','2','3'],
    producerIndex:0,
    testerIndex:0,
    status:0,
    statusList:[{name:'未通过',value:0,checked:true},{name:'通过',value:1,checked:false}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      dateType:options.dateType,
      startTime:options.startTime,
      endTime:options.endTime,
    })
    this.getNowDate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindscrolltolower(){
    this.data.pageNum++;
    this.getDetailInfo();
  },
  boxChange(e){
    console.log('boxChange',e);
    this.setData({
      boxType:e.detail.value,
      pageNum:1,
    })
    this.getDetailInfo();
  },

  versionChange(e){
    console.log('versionChange',e);
    this.setData({
      version:e.detail.value,
      pageNum:1,
    })
    this.getDetailInfo();
  },
  
  statusChange(e){
    console.log('statusChange',e);
    this.setData({
      status:e.detail.value,
      pageNum:1,
    })
    this.getDetailInfo();
  },

  producerChange(e){
    this.setData({
      producerIndex:e.detail.value,
      pageNum:1,
    })
    this.getDetailInfo();
  },

  testerChange(e){
    this.setData({
      testerIndex:e.detail.value,
      pageNum:1,
    })
    this.getDetailInfo();
  },

  startTimeChange(e){
    console.log('startTimeChange',e);
    this.setData({
      startTime:e.detail.value,
      pageNum:1,
    })
    this.getDetailInfo();
  },

  endTimeChange(e){
    console.log('endTimeChange',e);
    this.setData({
      endTime:e.detail.value,
      startTime:e.detail.value,
      pageNum:1,
    })
    this.getDetailInfo();
  },

  getDetailInfo:function(){
    let that = this;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/produce_list/produceDetailInfo',
        header: header,
        data:{
          pageNum:that.data.pageNum,
          pageSize:that.data.pageSize,
          startTime:that.data.startTime,
          endTime:that.data.endTime,
          produceUserId:that.data.producerIndex==0?'':that.data.producerList[that.data.producerIndex-1].produce_num,
          produceTestUserId:that.data.testerIndex==0?'':that.data.testerList[that.data.testerIndex-1].produce_num,
          isPass:that.data.status,
          pileVer:that.data.boxType,
          hpVer:that.data.version,
        },
        success: function (res) {
          if (res.data.result === 0 && res.data.data) {
            if(that.data.pageNum==1){
              that.setData({
                testList:res.data.data
              })
            }else {
              that.setData({
                testList:that.data.testList.concat(res.data.data)
              })
            }
          }
        }
      })
    });
  },

  /**
   * 获取控制盒版本列表
   */
  getBoxTypes() {
    let that = this;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/produce/pile_ver',
        header: header,
        success: function (res) {
          if (res.data.result === 0 && res.data.data) {
            let resData = res.data.data;
            that.setData({
              boxTypes:resData.pileVerList,
              boxType:resData.pileVerList[0],
              versionList:resData.verList,
              version:resData.verList[0],
            })
          }
        }
      })
    });
  },
  /**
   * 获取生产员
   */
  getProducer(){
    let that = this;

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/produce/produce_user',
        header: header,
        success: function (res) {
          if(res.data.result==0){
            let producerNameList = [];
            res.data.data.forEach(item=>{
              producerNameList.push(item.produce_name);
            })
            producerNameList.unshift('全部');
            that.setData({
              producerList:res.data.data,
              producerNameList:producerNameList
            })
            that.getTester();
          }else {
            wx.showToast({
              title: 'rst:网络异常',
              icon:'none'
            })
          }
          
        },
        fail: function (res) {
          wx.showToast({
            title: 'fail:网络异常',
            icon:'none'
          })
        }
      })

    })
  },

  /**
   * 获取检测员
   */
  getTester(){
    let that = this;

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/produce/produce_test_user',
        header: header,
        success: function (res) {
          console.log('getTester',res);
          if(res.data.result==0){
            let testerNameList = [];
            res.data.data.forEach(item=>{
              testerNameList.push(item.produce_name);
            })
            testerNameList.unshift('全部');
            that.setData({
              testerList:res.data.data,
              testerNameList:testerNameList
            })
            that.getDetailInfo();
          }else {
            wx.showToast({
              title: 'rst:网络异常',
              icon:'none'
            })
          }
          
        },
        fail: function (res) {
          wx.showToast({
            title: 'fail:网络异常',
            icon:'none'
          })
        }
      })

    })
  },
  
  getNowDate: function () {
    let reg = /\//g;
    let nowTime = new Date();
    let agoTime = new Date(nowTime.getTime()-1000*60*60*24*7);
    if(this.data.dateType=='single'){
      agoTime = nowTime;
    }
    let nowDate = util.formatTime(nowTime).substr(0, 10).replace(reg, '-');
    let agoDate = util.formatTime(agoTime).substr(0, 10).replace(reg, '-');
    this.setData({
      date:nowDate,
      // endTime: nowDate,
      // startTime: agoDate,
    })
  
    this.getProducer();
    this.getBoxTypes();
  },
})